package com.etheart.music.web.action.usersphere;

import java.util.Map;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.interceptor.ApplicationAware;
import org.apache.struts2.interceptor.RequestAware;
import org.apache.struts2.interceptor.SessionAware;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.etheart.model.entity.User;
import com.etheart.music.web.action.usersphere.helper.UsersphereHelper;
import com.etheart.usersphere.service.UserService;
import com.opensymphony.xwork2.ActionSupport;

@Controller("loginAction")
@Scope("prototype") 
public class LoginAction extends ActionSupport implements SessionAware,RequestAware ,ApplicationAware{
	private static final long serialVersionUID = 1L;
	private User user;
	@Autowired
	UserService userService;   
	
	@Action(value="login",results={@Result(name="success", location="/index.jsp"),  
            @Result(name="login", location="/usersphere/login.jsp")})  
	public String login() {
		User u = userService.getLogin(user.getUsername(),user.getPassword());
		if (u != null) {            
			UsersphereHelper.loginSession(session, u);
			return "success";
		}
		request.put("message", "登录失败，账号或密码不正确");
		return "login";
	}

	@Action(value="loginDialog",results={@Result(name="success", location="/usersphere/loginSuccess.jsp"),  
            		 @Result(name="login", location="/usersphere/loginDialog.jsp")})  
	public String dialog(){
		return login();  
	}
	
	Map<String, Object> request;
	Map<String, Object> session;
	Map<String, Object> application;
	public void setRequest(Map<String, Object> arg0) {
		this.request = arg0;
	}
	public void setSession(Map<String, Object> arg0) {
		this.session = arg0;
	}
	public void setApplication(Map<String, Object> arg0) {
		this.application=arg0;
	}
	public User getUser() {
		return user;
	}
	public void setUser(User user) {
		this.user = user;
	}
	
}
